/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package kitaplaner.application;

import java.awt.Component;
import java.sql.Date;
import java.sql.Time;
import javax.swing.JComboBox;
import javax.swing.JTextField;

/**
 *
 * @author kristina
 */
public class AdditionalApp {
    
    
    public Time string2Time(String time)
    {
        Time tTime = null;

        time = fitToTimeFormat(time);

        String [] aTime = time.split(":");

        try
        {
            if (aTime.length == 3)
            {
                tTime = new Time(Integer.parseInt(aTime[0]),
                                 Integer.parseInt(aTime[1]),
                                 Integer.parseInt(aTime[2]));
            }
            else if (aTime.length == 2)
            {
                tTime = new Time(Integer.parseInt(aTime[0]),
                                 Integer.parseInt(aTime[1]), 0);
            }
            else if (aTime.length == 1)
            {
                tTime = new Time(Integer.parseInt(aTime[0]), 0, 0);
            }
        }
        catch (NumberFormatException nfe)
        {
            tTime = new Time (0,0,0);
            nfe.printStackTrace();
        }

        return tTime;
    }

    public Date string2Date(String date)
    {
        Date dDate = null;

        if (date.contains("."))
        {
            String [] aTime = date.split(".");
            try
            {
                if (aTime.length == 3)
                {
                    dDate = new Date(Integer.parseInt(aTime[2]),
                                     Integer.parseInt(aTime[1]),
                                     Integer.parseInt(aTime[0]));
                }
                else if (aTime.length == 2)
                {
                    dDate = new Date(Integer.parseInt(aTime[1]),
                                     Integer.parseInt(aTime[0]), 0);
                }
                else if (aTime.length == 1)
                {
                    dDate = new Date(Integer.parseInt(aTime[0]), 0, 0);
                }
            }
            catch (NumberFormatException nfe)
            {
                dDate = new Date (0,0,0);
                nfe.printStackTrace();
            }
        }
        else if (date.contains("-"))
        {
            String [] aTime = date.split("-");

            try
            {
                if (aTime.length == 3)
                {
                    dDate = new Date(Integer.parseInt(aTime[0]),
                                     Integer.parseInt(aTime[1]),
                                     Integer.parseInt(aTime[2]));
                }
                else if (aTime.length == 2)
                {
                    dDate = new Date(Integer.parseInt(aTime[0]),
                                     Integer.parseInt(aTime[1]), 0);
                }
                else if (aTime.length == 1)
                {
                    dDate = new Date(Integer.parseInt(aTime[0]), 0, 0);
                }
            }
            catch (NumberFormatException nfe)
            {
                dDate = new Date (0,0,0);
                nfe.printStackTrace();
            }
        }
        else
        {
           dDate = new Date (0,0,0);
        }
        

        return dDate;
    }

    public int string2Int(String string)
    {
        int i = 0;

        try
        {
            i = Integer.parseInt(string);
        }
        catch (NumberFormatException nfe)
        {
            i = 1;
            nfe.printStackTrace();
        }

        return i;
    }

    public double string2Double(String string)
    {
        double d = 0;

        string = this.kommaZuPunkt(string);

        try
        {
            d = Double.parseDouble(string);
        }
        catch (NumberFormatException nfe)
        {
            d = 1;
            nfe.printStackTrace();
        }

        return d;
    }
    
    public void clearContainer(Component[] components)
    {
        // components which content should be cleared 
        JTextField jTextReset = new JTextField();
        JComboBox jComboReset = new JComboBox();
        
        try {
            for (int i = 0; i < components.length; i++) {
                if (components[i].getClass().getName() == jTextReset.getClass().getName()) {
                    jTextReset = (JTextField) components[i];
                    jTextReset.setText("");
                    
                } else if (components[i].getClass().getName() == jComboReset.getClass().getName()) {
                    jComboReset = (JComboBox) components[i];
                    if (jComboReset.getItemCount() > 0) {
                        jComboReset.setSelectedIndex(0);
                    }
                }

            }
        }catch(IllegalArgumentException iae)
        {
            iae.printStackTrace();
        }
    }
    
    public int getMitarbeiterIDFromCB(JComboBox combobox, MitarbeiterApp mitarbeiterController)
    {
        String[] mitarbeiter = combobox.getSelectedItem().toString().split(" ");
        int mitarbeiterID = mitarbeiterController.getMaByName(mitarbeiter[0], mitarbeiter[1]).getId();
        
        return mitarbeiterID;
    }

    public String kommaZuPunkt(String komma)
    {
        String punkt = "";
        
        punkt = komma.replace(",", ".");

        return punkt;
    }

    public String fitToTimeFormat(String time)
    {
        time = time.replace(".", ":");
        time = time.replace(" ", ":");
        time = time.replace(",", ":");
        time = time.replace(";", ":");

        return time;
    }
}
